package org.example;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ProducerDemo {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory(); //创建连接工厂
        factory.setHost("123.57.1.114"); //设置IP
        factory.setPort(5672); //前面说了，客户端与RabbitMQ Server连接使用的端口默认是5672
        factory.setVirtualHost("test"); //Virtual Host
        factory.setUsername("admin"); //用户名
        factory.setPassword("admin"); //密码
        //建立连接Connection
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        /**
         * queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
         *                                  Map<String, Object> arguments)
         * 参数说明：
         * queue 队列名称
         * durable 是否可持久化
         * exclusive 是否独占
         * autoDelete 是否自动删除
         * arguments 其他的配置信息
         */
        channel.queueDeclare("hello",true,false,false,null);
        for (int i = 0; i < 10; i++) {
            String msg = "hello rabbitmq" + i;
            channel.basicPublish("","hello",null,msg.getBytes());
        }
        System.out.println("消息发送成功");
        // 释放资源
//        channel.close();
//        connection.close();
    }
}
